package com.net.xpay.alipay.common;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.net.common.util.SerializationUtil;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Map;


@Slf4j
public class CommonAlipayClient {

    private final AlipayClient alipayClient;

    private CommonAlipayProperties commonAlipayProperties;


    public CommonAlipayClient(AlipayClient alipayClient, CommonAlipayProperties commonAlipayProperties) {
        this.alipayClient = alipayClient;
        this.commonAlipayProperties = commonAlipayProperties;
    }

    public boolean checkSignV1(Map<String, String> params) throws AlipayApiException {
        return AlipaySignature.verifyV1(params, commonAlipayProperties.getAlipayPublicKey(), "UTF-8", "RSA2");
    }

    private <T extends AlipayResponse> T autoRetryExecute(AlipayRequest<T> req) throws AlipayApiException {
        return execute(req);
    }

    public <T extends AlipayResponse> T execute(AlipayRequest<T> req)
            throws AlipayApiException {
        T alipayRsp = alipayClient.execute(req);
        log.info("execute alipayRsp={}", SerializationUtil.obj2String(alipayRsp));
        return alipayRsp;
    }

    private <T extends AlipayResponse> T pageExecute(AlipayRequest<T> req) throws AlipayApiException {
        T alipayRsp = alipayClient.pageExecute(req, "get");
        log.info("pageExecute alipayRsp={}", SerializationUtil.obj2String(alipayRsp));
        if (alipayRsp.isSuccess()) {
            return alipayRsp;
        }
        throw new AlipayApiException(alipayRsp.getSubCode(), alipayRsp.getSubMsg());
    }

    /**
     * 创建订单
     */
    public AlipayTradeCreateResponse createAlipayTrade(AlipayTradeCreateRequest request) throws AlipayApiException {
        return execute(request);
    }

    /**
     * 预创建订单 返回二维码
     * @return
     */
    public AlipayTradePrecreateResponse preCreateAlipayTrade(AlipayTradePrecreateRequest request) throws AlipayApiException {
        return execute(request);
    }

    /**
     * 电脑网站支付
     * @return
     */
    public AlipayTradePagePayResponse createAlipayTradePagePay(AlipayTradePagePayRequest request) throws AlipayApiException {
        return pageExecute(request);
    }

    /**
     * 订单退款
     */
    public AlipayTradeRefundResponse refundAlipayTrade(String tradeNo, BigDecimal refundAmount, String outRequestNo) throws AlipayApiException {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no", tradeNo);
        bizContent.put("refund_amount", refundAmount);
        bizContent.put("out_request_no", outRequestNo);
        request.setBizContent(bizContent.toString());

        return execute(request);
    }

    /**
     * 查询订单
     */
    public AlipayTradeQueryResponse queryAlipayTrade(String outTradeNo) throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
        request.setBizContent(bizContent.toString());
        return execute(request);
    }


}
